home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / lyrics / WinampcnParser.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  3.3 KB  |  104 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
  2. #
  3. # Copyright (C) 2007 Austin  <austiny@sohu.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  11. # GStreamer plugins to be used and distributed together with GStreamer
  12. # and Rhythmbox. This permission is above and beyond the permissions granted
  13. # by the GPL license by which Rhythmbox is covered. If you modify this code
  14. # you may extend this exception to your version of the code, but you are not
  15. # obligated to do so. If you do not wish to do so, delete this exception
  16. # statement from your version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  26.  
  27. import sys
  28. import urllib
  29. import gettext
  30. import re
  31. import rb
  32. from xml.dom import minidom
  33.  
  34. def detect_charset(s):
  35.     charsets = ('iso-8859-1', 'gbk', 'utf-8')
  36.     for charset in charsets:
  37.         try:
  38.             return unicode(unicode(s, 'utf-8').encode(charset), 'gbk')
  39.         except:
  40.             continue
  41.     return s
  42.  
  43. class WinampcnParser(object):
  44.     def __init__(self, artist, title):
  45.         self.artist = artist
  46.         self.title = title
  47.     
  48.     def search(self, callback, *data):
  49.  
  50.         # encode search string
  51.                 title_encode = urllib.quote(detect_charset(self.title).encode('gbk').replace(' ', ''))
  52.         artist_encode = urllib.quote(detect_charset(self.artist).encode('gbk').replace(' ',''))
  53.         url = 'http://www.winampcn.com/lyrictransfer/get.aspx?song=%s&artist=%s&lsong=%s&Datetime=20060601' % (title_encode, artist_encode, title_encode)
  54.         
  55.         loader = rb.Loader()
  56.         loader.get_url (url, self.got_lyrics, callback, *data)
  57.         
  58.     def got_lyrics(self, xmltext, callback, *data):
  59.         # retrieve xml content
  60.         if xmltext is None:
  61.             callback (None, *data)
  62.             return
  63.  
  64.         try:
  65.             xmltext = xmltext.decode('gbk').encode('UTF-8')
  66.             xmltext = xmltext.replace('encoding="gb2312"', 'encoding="UTF-8"')
  67.             xmldoc = minidom.parseString(xmltext)
  68.             root = xmldoc.documentElement
  69.  
  70.             lrcurl = root.getElementsByTagName('LyricUrl')[0].childNodes[0].data
  71.             if lrcurl is None:
  72.                 callback (xmltext, *data)
  73.                 return
  74.  
  75.             # download the lyrics file
  76.             lrcurl_encode = urllib.quote(detect_charset(lrcurl).encode('gbk'))
  77.             lrcurl_encode = lrcurl_encode.replace('%3A', ':');
  78.  
  79.             loader = rb.Loader()
  80.             loader.get_url (lrcurl_encode, self.parse_lyrics, callback, *data)
  81.         except:
  82.             callback (None, *data)
  83.  
  84.  
  85.     def parse_lyrics(self, lyrics, callback, *data):
  86.  
  87.         if lyrics is None:
  88.             callback (None, *data)
  89.             return
  90.  
  91.         # transform it into plain text
  92.         lrcplaintext = lyrics
  93.         try:
  94.             lrcplaintext = re.sub('\[.*?\]', '', lrcplaintext)
  95.             lrcplaintext = lrcplaintext.decode('gbk').encode('UTF-8')
  96.         except:
  97.             callback (lrcplaintext, *data)
  98.             return
  99.  
  100.         # callback and show
  101.         lrcplaintext += "\n\nLyrics provided by winampcn.com"
  102.         callback(lrcplaintext, *data)
  103.  
  104.